###################################################
#                                                 #
#          PREPARA LOS DATOS PARA MOSAR           #
#      Sardina común y Anchoveta Centro-Sur       #         
#                                                 #
#                  Luis Cubillos                  #
#          Departamento de Oceanografía           #
#            Universidad de Concepción            #
#                                                 #
###################################################
rm(list=ls())
debug <- 0
DatDir <- "/Users/luiscubillos/ADMwork/MOSAR/FishStock/Datos"
ComDir <- "/Users/luiscubillos/ADMwork/MOSAR/FishStock/ComdsR/Envi.R"
BiolDir <-"/Users/luiscubillos/ADMwork/MOSAR/FishStock/BiolParms/LifeHistParms.R"
sOutDir <-"/Users/luiscubillos/ADMwork/MOSAR/FishStock/Sben" 
aOutDir <-"/Users/luiscubillos/ADMwork/MOSAR/FishStock/Erin" 
#######################################################
source(BiolDir)
source(ComDir)
setwd(DatDir)
#Lectura de datos
#Time Series
	TheDataTS <- read.csv("MOSARTSdata1991-2010.csv",header=T)
	nmo  <- max(TheDataTS[,1]);nmo
	styr <- min(TheDataTS[,2]);styr
	endyr<- max(TheDataTS[,2]);endyr
	nyr  <- nmo/12;nyr
#LFD fishery sardine
	sFishLFD <- read.csv("MOSARSCCatchAtLength.csv")
	sReclasLFD <- read.csv("MOSARSCRECLASLFD.csv")
	sPelaceLFD <- read.csv("MOSARSCPELACESLFD.csv")

#LFD fishery anchovy
	aFishLFD <- read.csv("MOSARSCCatchAtLength.csv")
	aReclasLFD <- read.csv("MOSARSCRECLASLFD.csv")
	aPelaceLFD <- read.csv("MOSARSCPELACESLFD.csv")
#Escribe los datos
mylist <- c("Sardina","Anchoveta")
what2do <- (guiDlgList(mylist))
if(what2do=="Sardina"){
	setwd(sOutDir)
	outfile <- "sben2010.dat"
	#Select sardine data
	sppDataTS <- TheDataTS[,c(1:3,6,10,12,20,24,26,28,29)]
		#select positive data
    LFD1 <- sFishLFD[sFishLFD$TOTAL>0,,]
    LFD1obs <- dim(LFD1)[1]
    LFD1tin <- t(as.matrix(LFD1[,1]))
    LFD1 <- LFD1[c(5:37)]
    LFD2 <- sReclasLFD[sReclasLFD$TOTAL>0,,]
    LFD2obs <- dim(LFD2)[1]
    LFD2tin <- t(as.matrix(LFD2[,1]))
    LFD2 <- LFD2[c(5:37)]
    LFD3 <- sPelaceLFD[sPelaceLFD$TOTAL>0,,]
    LFD3obs <- dim(LFD3)[1]
    LFD3tin <- t(as.matrix(LFD3[,1]))
    LFD3 <- LFD3[c(5:37)]
    #Biol
    nages <- 4 #
    M <- sM
    Linf <- sLinf
    K <- sK
    t0 <- st0
    age1 <- 3/12 #sage1
    CVageP1 <- sCVageP1
    CVageP2 <- sCVageP2
    amat <- samat
    bmat <- sbmat
    #Number of cohorts
    nco <- (nmo)/12+(nages-1)
    #Recruitment and surviving cohorts (assuming age=4)
    mes.rec <- t(as.matrix(c(1,1,1,seq(1,nmo,12))));mes.rec
    mes.surv <-t(as.matrix(c(seq(12,nmo,12),nmo,nmo,nmo)))
    minageco <- t(as.matrix(c(37,25,14,rep(3,nco-3)))) 
    }
if(what2do=="Anchoveta"){
	setwd(aOutDir)
	outfile <- "erin2010.dat"
		#select positive data
	sppDataTS <- TheDataTS[,c(1:3,7,11,16,22,25,27,30,31)]
    LFD1 <- aFishLFD[aFishLFD$TOTAL>0,,]
    LFD1obs <- dim(LFD1)[1]
    LFD1tin <- t(as.matrix(LFD1[,1]));LFD1tin
    LFD1 <- LFD1[c(5:37)]
    LFD2 <- aReclasLFD[aReclasLFD$TOTAL>0,,]
    LFD2obs <- dim(LFD2)[1]
    LFD2tin <- t(as.matrix(LFD2[,1]))
    LFD2 <- LFD2[c(5:37)]
    LFD3 <- aPelaceLFD[aPelaceLFD$TOTAL>0,,]
    LFD3obs <- dim(LFD3)[1]
    LFD3tin <- t(as.matrix(LFD3[,1]))
    LFD3 <- LFD3[c(5:37)]
    #Biol
    nages <- 4
    M <- aM
    Linf <- aLinf
    K <- aK
    t0 <- at0
    age1 <- 3/12 #aage1
    CVageP1 <- aCVageP1
    CVageP2 <- aCVageP2
    amat <- aamat
    bmat <- abmat
    #Number of cohorts
    nco <- (nmo)/12+(nages-1);nco
    #Recruitment and surviving cohorts (assuming age=4)
    mes.rec <- t(as.matrix(c(1,1,1,seq(1,nmo,12))));mes.rec
    mes.surv <-t(as.matrix(c(seq(12,nmo,12),nmo,nmo,nmo)));mes.surv
    minageco <- t(as.matrix(c(37,25,13,rep(3,nco-3))));minageco 
    }

write("#MOSARv1.0",outfile)
write("#Debug",outfile,append=T)
write(debug,outfile,append=T)

write("#nmo",outfile,append=T)
write(nmo,outfile,append=T)

write("#nyr",outfile,append=T)
write(nyr,outfile,append=T)

write("#styr",outfile,append=T)
write(styr,outfile,append=T)

write("#endyr",outfile,append=T)
write(endyr,outfile,append=T)

write("#Timeseriesdata",outfile,append=T)
write.table(sppDataTS,outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#LFD",outfile,append=T)
write("#stlen",outfile,append=T)
write(3,outfile,append=T)

write("#steplen",outfile,append=T)
write(0.5,outfile,append=T)

write("#nlen",outfile,append=T)
write(33,outfile,append=T)

write("#nobs_fish_len_comp",outfile,append=T)
write(LFD1obs,outfile,append=T)

write("#Obsmonths_fish_len_comp",outfile,append=T)
write(LFD1tin,outfile,append=T)

write("#ObsFishLFD",outfile,append=T)
write.table(LFD1,outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#nobs_reclas_len_comp",outfile,append=T)
write(LFD2obs,outfile,append=T)

write("#Obsmonths_reclas_len_comp",outfile,append=T)
write(LFD2tin,outfile,append=T)

write("#ObsReclasLFD",outfile,append=T)
write.table(LFD2,outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

write("#nobs_pelaces_len_comp",outfile,append=T)
write(LFD3obs,outfile,append=T)

write("#Obsmonths_pelaces_len_comp",outfile,append=T)
write(LFD3tin,outfile,append=T)

write("#ObsPelacesLFD",outfile,append=T)
write.table(LFD3,outfile,append=T,sep=" ",eol="\n",col.names=F,row.names=F)

##BIOL
write("#Nages",outfile,append=T)
write(nages,outfile,append=T)

write("#NaturalMort",outfile,append=T)
write(M,outfile,append=T)

write("#AsympLength",outfile,append=T)
write(Linf,outfile,append=T)

write("#GrowthCoeff",outfile,append=T)
write(K,outfile,append=T)
write("#GrowthTzero",outfile,append=T)
write(t0,outfile,append=T)

write("#Age1",outfile,append=T)
write(age1,outfile,append=T)

write("#CVageParms",outfile,append=T)
write(CVageP1,outfile,append=T)
write(CVageP2,outfile,append=T)

write("#MaturityParms",outfile,append=T)
write(amat,outfile,append=T)
write(bmat,outfile,append=T)

write("#Numcohorts",outfile,append=T)
write(nco,outfile,append=T)
write("#Month_Rec_cohorts",outfile,append=T)
write(mes.rec,outfile,append=T)
write("#Month_Surv_cohorts",outfile,append=T)
write(mes.surv,outfile,append=T)
write("#Min_Agemo_cohorts",outfile,append=T)
write(minageco,outfile,append=T)

write("#eof",outfile,append=T)
write(999,outfile,append=T)
